package gsredis

import (
	"strconv"
	"time"

	"gitee.com/crack007/goose/gslog"

	"github.com/go-redis/redis"
)

var RedisManager *redisManager

func newRedisManager() *redisManager {
	return &redisManager{
		dbMap: make(map[string]*redis.Client),
	}
}

type redisManager struct {
	dbMap map[string]*redis.Client
}

func (x *redisManager) initNode(nodeName string, config *RedisConfig) {
	gslog.Info("init node: %s", nodeName)
	x.dbMap[nodeName] = redis.NewClient(&redis.Options{
		Addr:         config.Ip + ":" + strconv.Itoa(config.Port),
		Password:     config.Password,
		DB:           config.Database,
		MinIdleConns: 1,
		MaxConnAge:   time.Duration(config.MaxConnLifetime) * time.Second,
		IdleTimeout:  time.Duration(config.IdleTimeout) * time.Second,
		PoolSize:     config.PoolSize,
	})
}

func (x *redisManager) init(configMap map[string]*RedisConfig) {
	for nodeName, config := range configMap {
		x.initNode(nodeName, config)
	}

	gslog.Info("redis init finish!")
}
